package com.ruoyi.supervise.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.config.HnCityBadConfig;
import com.ruoyi.supervise.config.HnCityGoodConfig;
import com.ruoyi.supervise.config.HnCityPeopleBadConfig;
import com.ruoyi.supervise.config.HnCityPeopleGoodConfig;
import com.ruoyi.supervise.domain.HnCityBad;
import com.ruoyi.supervise.domain.HnCityGood;
import com.ruoyi.supervise.domain.HnCityPeopleBad;
import com.ruoyi.supervise.domain.HnCityPeopleGood;
import com.ruoyi.supervise.domain.IGMCompanyPoorInfo;
import com.ruoyi.supervise.domain.IGMCompanyWellInfo;
import com.ruoyi.supervise.domain.IGMPersonalPoorInfo;
import com.ruoyi.supervise.domain.IGMPersonalWellInfo;
import com.ruoyi.supervise.mapper.IntegrityInformationMapper;
import com.ruoyi.supervise.service.IIntegrityInformationService;

import cn.hutool.core.util.StrUtil;


/**
 * @ClassName: IntegrityInformationServiceImpl
 * @Description: 诚信信息管理中，企业和人员，良好不良行为记录impl
 * @Author wtj
 * @DateTime 2020年5月11日 上午10:05:29
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class IntegrityInformationServiceImpl implements IIntegrityInformationService 
{
    @Autowired
    private IntegrityInformationMapper integrityInformationMapper;

    @Autowired
    private HnCityGoodConfig hnCityGoodConfig;
    
    @Autowired
    private HnCityBadConfig hnCityBadConfig;
    
    @Autowired
    private HnCityPeopleGoodConfig hnCityPeopleGoodConfig;
    
    @Autowired
    private HnCityPeopleBadConfig hnCityPeopleBadConfig;
    
	@Override
	public List<IGMCompanyWellInfo> selectIGMCompanyWellInfoList(IGMCompanyWellInfo igmcwi) {
		return integrityInformationMapper.selectIGMCompanyWellInfoList(igmcwi);
	}

	@Override
	public IGMCompanyWellInfo selectIGMCompanyWellInfoByGuid(String guid) {
		IGMCompanyWellInfo igmcwi = integrityInformationMapper.selectIGMCompanyWellInfoByGuid(guid);
		if(igmcwi!=null) {
			String[] subStr=StrUtil.split(igmcwi.getMaterials(), "|");
			if(subStr.length == 4) {
				String[] file = StrUtil.split(subStr[1], "/");
				if(file.length >= 3) {
	        		if("profile".equals(file[1]) && "lingshui".equals(file[2]) && StrUtil.containsAny(subStr[1], "/profile/lingshui/")) {
	            		igmcwi.setFileSort("1");
	            		igmcwi.setMaterials(subStr[1]);
	            		igmcwi.setFileName(subStr[2]);
	            	}else {
	            		igmcwi.setFileSort("2");
	            		igmcwi.setMaterials("");
	            	}
	        	}else if(StrUtil.containsAny(subStr[1],"\\")){
	        		if(StrUtil.split(subStr[1], "\\").length == 4) {
	        			igmcwi.setFileSort("0");
	            		igmcwi.setMaterials(Global.getConfig("ruoyi.bigPlatform") + subStr[1]);
	        		}else {
	        			igmcwi.setFileSort("2");
	            		igmcwi.setMaterials("");
	        		}
	        	}
			}else {
				igmcwi.setFileSort("2");
        		igmcwi.setMaterials("");
			}
		}
		return igmcwi;
	}

	@Override
	public List<IGMCompanyPoorInfo> selectIGMCompanyPoorInfoList(IGMCompanyPoorInfo igmcpi) {
		// TODO Auto-generated method stub
		return integrityInformationMapper.selectIGMCompanyPoorInfoList(igmcpi);
	}

	@Override
	public IGMCompanyPoorInfo selectIGMCompanyPoorInfoByGuid(String guid) {
		IGMCompanyPoorInfo igmcpi = integrityInformationMapper.selectIGMCompanyPoorInfoByGuid(guid);
		if(igmcpi!=null) {
			String[] subStr=StrUtil.split(igmcpi.getMaterials(), "|");
			if(subStr.length == 4) {
				String[] file = StrUtil.split(subStr[1], "/");
				if(file.length >= 3) {
	        		if("profile".equals(file[1]) && "lingshui".equals(file[2]) && StrUtil.containsAny(subStr[1], "/profile/lingshui/")) {
	        			igmcpi.setFileSort("1");
	        			igmcpi.setMaterials(subStr[1]);
	        			igmcpi.setFileName(subStr[2]);
	            	}else {
	            		igmcpi.setFileSort("2");
	            		igmcpi.setMaterials("");
	            	}
	        	}else if(StrUtil.containsAny(subStr[1],"\\")){
	        		if(StrUtil.split(subStr[1], "\\").length == 4) {
	        			igmcpi.setFileSort("0");
	        			igmcpi.setMaterials(Global.getConfig("ruoyi.bigPlatform") + subStr[1]);
	        		}else {
	        			igmcpi.setFileSort("2");
	        			igmcpi.setMaterials("");
	        		}
	        	}
			}else {
				igmcpi.setFileSort("2");
				igmcpi.setMaterials("");
			}
		}
		return igmcpi;
	}

	@Override
	public List<HnCityGood> statisticsCompanyWellInfo() {
		List<HnCityGood> hnCityGoodList = integrityInformationMapper.statisticsCompanyWellInfoInProvince();
		if(!StringUtils.isEmpty(hnCityGoodList)) {
			for(HnCityGood good:hnCityGoodConfig.getHncityList()) {
				for(HnCityGood hncg:hnCityGoodList) {
					if(good.getHncityName().equals(hncg.getHncityName())) {
						good.setHncityNum(hncg.getHncityNum());
					}
				}
			}
		}
		HnCityGood hncg = new HnCityGood();
		hncg.setHncityNo("23");
		hncg.setHncityName("省外");
		hncg.setHncityNum(integrityInformationMapper.statisticsCompanyWellInfoOutProvince());
		hnCityGoodConfig.getHncityList().add(hncg);
		return hnCityGoodConfig.getHncityList();
	}

	@Override
	public List<HnCityBad> statisticsCompanyPoorInfo() {
		List<HnCityBad> hnCityBadList = integrityInformationMapper.statisticsCompanyPoorInfoInProvince();
		if(!StringUtils.isEmpty(hnCityBadList)) {
			for(HnCityBad bad:hnCityBadConfig.getHncityList()) {
				for(HnCityBad hncb:hnCityBadList) {
					if(bad.getHncityName().equals(hncb.getHncityName())) {
						bad.setHncityNum(hncb.getHncityNum());
					}
				}
			}
		}
		HnCityBad hncb = new HnCityBad();
		hncb.setHncityNo("23");
		hncb.setHncityName("省外");
		hncb.setHncityNum(integrityInformationMapper.statisticsCompanyPoorInfoOutProvince());
		hnCityBadConfig.getHncityList().add(hncb);
		return hnCityBadConfig.getHncityList();
	}

	@Override
	public List<IGMPersonalWellInfo> selectIGMPersonalWellInfoList(IGMPersonalWellInfo igmpwi) {
		List<IGMPersonalWellInfo> igmpwiList = integrityInformationMapper.selectIGMPersonalWellInfoList(igmpwi);
		return igmpwiList;
	}

	@Override
	public List<IGMPersonalPoorInfo> selectIGMPersonalPoorInfoList(IGMPersonalPoorInfo igmppi) {
		List<IGMPersonalPoorInfo> igmppiList = integrityInformationMapper.selectIGMPersonalPoorInfoList(igmppi);
		return igmppiList;
	}

	@Override
	public List<HnCityPeopleGood> statisticsIGMPersonalWellInfo() {
		List<HnCityPeopleGood> gbcList = integrityInformationMapper.statisticsIGMPersonalWellInfoGroupByCity();
		if(!StringUtils.isEmpty(gbcList)) {
			for(HnCityPeopleGood good:hnCityPeopleGoodConfig.getHncityList()) {
				for(HnCityPeopleGood gbc:gbcList) {
					if(good.getHncityName().equals(gbc.getHncityName())) {
						good.setHncityNum(gbc.getHncityNum());
					}
				}
			}
			int inProvinceTotal = 0;
			for(HnCityPeopleGood good:hnCityPeopleGoodConfig.getHncityList()) {
				inProvinceTotal = inProvinceTotal+good.getHncityNum();
			}
			int provinceTotal = integrityInformationMapper.statisticsIGMPersonalWellInfoTotal();
			HnCityPeopleGood good = new HnCityPeopleGood();
			good.setHncityNo("23");
			good.setHncityName("省外");
			good.setHncityNum(provinceTotal-inProvinceTotal);
			hnCityPeopleGoodConfig.getHncityList().add(good);
		}
		return hnCityPeopleGoodConfig.getHncityList();
	}

	@Override
	public List<HnCityPeopleBad> statisticsIGMPersonalPoorInfo() {
		List<HnCityPeopleBad> gbpaList = integrityInformationMapper.statisticsIGMPersonalPoorInfoGroupByPublishArea();
		if(!StringUtils.isEmpty(gbpaList)) {
			for(HnCityPeopleBad bad:hnCityPeopleBadConfig.getHncityList()) {
				for(HnCityPeopleBad gbpa:gbpaList) {
					if(bad.getHncityName().equals(gbpa.getHncityName())) {
						bad.setHncityNum(gbpa.getHncityNum());
					}
				}
			}
			int inProvinceTotal = 0;
			for(HnCityPeopleBad bad:hnCityPeopleBadConfig.getHncityList()) {
				inProvinceTotal = inProvinceTotal + bad.getHncityNum();
			}
			int provinceTotal = integrityInformationMapper.statisticsIGMPersonalPoorInfoTotal();
			HnCityPeopleBad bad = new HnCityPeopleBad();
			bad.setHncityNo("23");
			bad.setHncityName("省外");
			bad.setHncityNum(provinceTotal-inProvinceTotal);
			hnCityPeopleBadConfig.getHncityList().add(bad);
		}
		return hnCityPeopleBadConfig.getHncityList();
	}

}
